<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>get请求-简写</button>
  <button>post请求-简写</button>
  <button>get请求-完整</button>
  <button>post请求-完整</button>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script>
    // 简写
    document.querySelectorAll('button')[0].addEventListener('click', () => {
      axios.get("http://localhost:3000/api/getasync2").then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(err => {
        console.log(err);
      })
    })
    document.querySelectorAll('button')[1].addEventListener('click', () => {
      axios.post("http://localhost:3000/api/postmsg", {
        empno: '20240102',
        ename: '田振齐',
        job: '鸿蒙攻城狮',
        hiredate: '2024-1-2'
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(err => {
        console.log(err);
      })
    })
    document.querySelectorAll('button')[2].addEventListener('click', () => {
      axios({
        // url: 'http://localhost:3000/api/getasync2?username=admin&password=123',
        url: 'http://localhost:3000/api/getasync2',
        method: "GET",
        params: {
          username: "admin",
          password: "123456"
        }
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(err => {
        console.log(err);
      })
    })
    document.querySelectorAll('button')[3].addEventListener('click', () => {
      axios({
        // url: 'http://localhost:3000/api/getasync2?username=admin&password=123',
        url: '/postmsg',
        method: "POST",
        baseURL: 'http://localhost:3000/api',
        data: {
          username: "admin",
          password: "123456",
          age: 20
        }
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(err => {
        console.log(err);
      })
    })


  </script>
</body>

</html>